package cn.xopencode.appletweb.controller.permission;

import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.oss.rpc.permission.ResourceRpc;
import cn.xopencode.oss.rpc.permission.dto.ResourceCreateDTO;
import cn.xopencode.oss.rpc.permission.dto.ResourceUpdateDTO;
import cn.xopencode.oss.rpc.permission.vo.ResourceTreeNodeVO;
import cn.xopencode.oss.rpc.permission.vo.ResourceVO;
import cn.xopencode.security.annotations.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

//@Api(tags = "权限资源 API")
//@RestController
//@RequestMapping("/resource")
public class ResourceController {
    @DubboReference(version = "${dubbo.consumer.ResourceRpc.version}")
    private ResourceRpc resourceRpc;
    //@PostMapping("/create")
    @ApiOperation("创建资源")
    @RequiresPermissions("oss:resource:create")
    public CommonResult<String> createResource(@Valid ResourceCreateDTO createDTO) {
        return resourceRpc.createResource(createDTO);
    }

    //@PostMapping("/update")
    @ApiOperation("更新资源")
    @RequiresPermissions("system:resource:update")
    public CommonResult<Boolean> updateResource(@Valid ResourceUpdateDTO updateDTO) {
        return resourceRpc.updateResource(updateDTO);
    }

    //@PostMapping("/delete")
    @ApiOperation("删除资源")
    @ApiImplicitParam(name = "resourceId", value = "资源编号", required = true)
    @RequiresPermissions("oss:resource:delete")
    public CommonResult<Boolean> deleteResource(@RequestParam("resourceId") String resourceId) {
        return resourceRpc.deleteResource(resourceId);
    }

    //@GetMapping("/get")
    @ApiOperation("获得资源")
    @RequiresPermissions("oss:resource:tree")
    public CommonResult<ResourceVO> getResource(@RequestParam("resourceId") String resourceId) {
        return resourceRpc.getResource(resourceId);
    }

    //@GetMapping("/list")
    @ApiOperation("获得资源列表")
    @ApiImplicitParam(name = "resourceIds", value = "资源编号列表", required = true)
    @RequiresPermissions("oss:resource:tree")
    public CommonResult<List<ResourceVO>> listResources(@RequestParam("resourceIds") List<String> resourceIds) {
        return resourceRpc.listResources(resourceIds);
    }

    //@GetMapping("/tree")
    @ApiOperation("获得资源树")
    @RequiresPermissions("oss:resource:tree")
    public CommonResult<List<ResourceTreeNodeVO>> treeResource() {
        return resourceRpc.treeResource();
    }
}
